package edu.tamu.cs.studio;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameThread extends Thread 
{
	private boolean running;
	private GameView gamePanel;
	private SurfaceHolder surfaceHolder;
	private static final String TAG = GameThread.class.getSimpleName();
	public GameThread(SurfaceHolder surfaceHolder, GameView gamePanel)
	{
		super();
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;
	}
	public void setRunning(boolean running)
	{
		this.running = running;
	}
	@Override
	public void run() 
	{
		Canvas canvas;
		Log.d(TAG, "Starting game loop");
		while (running) 
		{
			canvas = null;
			// Lock the canvas on the surface
			try 
			{
				canvas = this.surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) 
				{
					// update game state
					// render state to the screen
					// draws the canvas on the panel
					this.gamePanel.onDraw(canvas);
				}
			} 
			finally
			{
				// in case of an exception the surface is not left in
				// an inconsistent state
				if (canvas != null)
				{
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			} // end finally
		}//End while
	}//End run()

}
